"""
The MIT License (MIT)
Copyright © 2022 Walkline Wang (https://walkline.wang)
Gitee: https://gitee.com/walkline/espnow-remote-controller
"""
from machine import Pin, SPI
from utime import sleep_ms
from drivers.ssd1306 import SSD1306_SPI
from config import Config

class OLED0_91(object):
	BAUDRATE = 80_000_000 # 使用硬件 SPI 驱动 SSD1306 OLED 必须使用此波特率，为什么？

	def __init__(self):
		self.display_width = 128
		self.display_height = 32

		self.__spi = SPI(
			1,
			baudrate=self.BAUDRATE,
			sck=Pin(Config.PINS.OLED.SCLK),
			mosi=Pin(Config.PINS.OLED.SDIN)
		)

		self.__oled = SSD1306_SPI(
			self.display_width, self.display_height,
			spi=self.__spi,
			dc=Pin(Config.PINS.OLED.DC),
			res=Pin(Config.PINS.OLED.RES)
		)

	def show(self):
		self.__oled.show()
	
	def clean(self):
		self.__oled.fill(0)
		self.show()
	
	def demo(self):
		for x in range(1, int(self.display_width * 1.5), 4):
			self.__oled.line(0, x, x, 0, 1)
			self.show()

			sleep_ms(20)

		# self.__oled.fill_rect(0, 0, 128, 32, 1)
		# self.show()


def run_test():
	oled = OLED0_91()
	oled.demo()


if __name__ == '__main__':
	try:
		run_test()
	except KeyboardInterrupt:
		pass
